<template>
  <el-dialog
    :visible.sync="visible"
    :title="$t('add')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="楼盘" prop="dictId">
            <el-select v-model="dataForm.dictId" filterable placeholder="关键字搜索" style="width:100%">
              <el-option
                v-for="item in dictList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
                @click.native="LPlistChange(item)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="座栋" prop="zdId">
            <el-select v-model="dataForm.zdId" @change="ZDlistChange" style="width:100%">
              <el-option v-for="item in zdList" :key="item.id" :label="item.num" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="单元" prop="dyId">
            <el-select v-model="dataForm.dyId" @change="DYlistChange" style="width:100%">
              <el-option
                v-for="item in dyList"
                :key="item.id"
                :label="item.num+'单元'"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="房间" prop="fjId">
            <el-select v-model="dataForm.fjId" @change="FJlistChange" style="width:100%">
              <el-option v-for="item in fjList" :key="item.id" :label="item.num" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="房号" prop="num">
        <el-input v-model="dataForm.num" placeholder="房号" disabled></el-input>
      </el-form-item>
      <el-form-item label="物业类型" prop="type">
        <ren-select
          v-model="dataForm.type"
          dict-type="dictType"
          placeholder="物业类型"
          :disabled="true"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="交易类型" prop="transactionType">
        <ren-select
          v-model="dataForm.transactionType"
          dict-type="houseTransactionType"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="房源类型" prop="houseType">
        <ren-select
          v-model="dataForm.houseType"
          dict-type="houseType"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="房源归类" prop="sortType">
        <ren-select
          v-model="dataForm.sortType"
          dict-type="houseSortType"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="行政区域" prop="districtCode">
        <el-select v-model="dataForm.districtCode" disabled style="width:100%" placeholder="行政区域">
          <el-option
            v-for="item in this.$store.state.CityRegion"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商圈" prop="business">
        <el-select v-model="dataForm.business" style="width:100%" placeholder="商圈" disabled>
          <el-option
            v-for="item in this.$store.state.BusinessPage"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="房屋现状" prop="situation">
        <ren-select
          v-model="dataForm.situation"
          dict-type="dictSituation"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <!-- <el-form-item label="业主姓名" prop="ownerName">
                <el-input v-model="dataForm.ownerName" placeholder="业主姓名"></el-input>
            </el-form-item>
            <el-form-item label="业主电话1" prop="ownerPhone1">
                <el-input v-model="dataForm.ownerPhone1" placeholder="业主电话"></el-input>
            </el-form-item>
            <el-form-item label="业主电话2" prop="ownerPhone2">
                <el-input v-model="dataForm.ownerPhone2" placeholder="业主电话"></el-input>
      </el-form-item>-->
      <el-form-item label="面积" prop="area">
        <el-input v-model="dataForm.area" placeholder="面积" @change="Setprices"></el-input>
      </el-form-item>
      <el-form-item label="单价" prop="price">
        <el-input v-model="dataForm.price" placeholder="单价" @change="Setprices"></el-input>
      </el-form-item>
      <el-form-item label="总价" prop="prices">
        <el-input v-model="dataForm.prices" placeholder="总价"></el-input>
      </el-form-item>

      <!-- <el-form-item label="钥匙" prop="key">
                <el-input v-model="dataForm.key" placeholder="钥匙"></el-input>
      </el-form-item>-->
      <!-- <el-form-item label="图片情况" prop="image">
                <el-input v-model="dataForm.image" placeholder="图片情况"></el-input>
      </el-form-item>-->
      <el-form-item label="装修情况" prop="renovation">
        <ren-select
          v-model="dataForm.renovation"
          dict-type="dictRenovation"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="家具情况" prop="furniture">
        <el-select v-model="dataForm.furniture" class="FormSelect" style="width:100%">
          <el-option label="没有" value="0"></el-option>
          <el-option label="有" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="看房方式" prop="lookMode">
        <ren-select
          v-model="dataForm.lookMode"
          dict-type="dictLookMode"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="房子来源" prop="source">
        <ren-select
          v-model="dataForm.source"
          dict-type="houseSource"
          class="FormSelect"
          style="width:100%"
        ></ren-select>
      </el-form-item>
      <el-form-item label="备注" prop="remake" class="FormitemW100">
        <el-input v-model="dataForm.remark" placeholder="备注" type="textarea" :rows="2"></el-input>
      </el-form-item>
      <!-- <el-form-item label="最新跟进" prop="followTime">
                <el-input v-model="dataForm.followTime" placeholder="最新跟进"></el-input>
            </el-form-item>
            <el-form-item label="维护人" prop="maintain">
                <el-input v-model="dataForm.maintain" placeholder="维护人"></el-input>
            </el-form-item>
            <el-form-item label="部门id" prop="deptId">
                <el-input v-model="dataForm.deptId" placeholder="部门id"></el-input>
            </el-form-item>
            <el-form-item label="审核状态" prop="examine">
                <el-input v-model="dataForm.examine" placeholder="审核状态"></el-input>
            </el-form-item>
            <el-form-item label="外网同步" prop="extranet">
                <el-input v-model="dataForm.extranet" placeholder="外网同步"></el-input>
            </el-form-item>
            <el-form-item label="城市租户编码" prop="tenantCode">
                <el-input v-model="dataForm.tenantCode" placeholder="城市租户编码"></el-input>
            </el-form-item>
            <el-form-item label="修改人" prop="updater">
                <el-input v-model="dataForm.updater" placeholder="修改人"></el-input>
            </el-form-item>
            <el-form-item label="修改时间" prop="updateDate">
                <el-input v-model="dataForm.updateDate" placeholder="修改时间"></el-input>
      </el-form-item>-->
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        dictId: "",
        zdId: "",
        dyId: "",
        fjId: "",
        num: "",
        type: "",
        businessType: "",
        transactionType: "",
        houseType: "",
        sortType: "",
        districtCode: "",
        business: "",
        situation: "",
        ownerName: "",
        ownerPhone1: "",
        ownerPhone2: "",
        area: "",
        price: "",
        prices: "",
        key: "",
        image: "",
        renovation: "",
        furniture: "",
        lookMode: "",
        source: "",
        followTime: "",
        maintain: "",
        deptId: "",
        examine: "",
        extranet: "",
        tenantCode: "",
        creator: "",
        createDate: "",
        updater: "",
        updateDate: "",
        remark: ""
      },
      // options: [],
      // value: [],
      // list: [],
      // loading: false
      dictList: [],
      zdList: [],
      dyList: [],
      fjList: []
    };
  },
  computed: {},
  created() {
    this.getDictList();
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/house/houseinfo/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
        })
        .catch(() => {});
    },

    //获取楼盘列表
    getDictList() {
      let _this = this;
      this.$http
        .get(`/house/houseinfo/dictList`, {
          params: {
            name: ""
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dictList = res.data;
        })
        .catch(() => {});
    },
    //楼盘列表change
    LPlistChange(item) {
      // this.$refs["dataForm"].resetFields();
      this.zdList = [];
      this.dyList = [];
      this.fjList = [];
      this.dataForm.dictId = item.id;
      this.getZdList(item.id);
      this.dataForm.type = item.type;
      this.dataForm.districtCode = item.districtCode;
      this.dataForm.business = item.business;
    },
    //获取楼盘座栋列表
    getZdList(id) {
      let _this = this;
      this.zdList = [];
      this.dataForm.zdId = "";
      this.$http
        .get(`/house/houseinfo/zdList`, {
          params: {
            dictId: id,
            num: ""
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.zdList = res.data;
        })
        .catch(() => {});
    },

    //座栋change
    ZDlistChange(val) {
      this.getDyList(val);
    },

    //获取楼盘座栋单元列表
    getDyList(id) {
      let _this = this;
      this.dyList = [];
      this.dataForm.dyId = "";
      this.$http
        .get(`/house/houseinfo/dyList`, {
          params: {
            zdId: id
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dyList = res.data;
        })
        .catch(() => {});
    },

    //单元列表change
    DYlistChange(val) {
      this.getFjList(val);
    },

    //获取单元房间列表
    getFjList(id) {
      let _this = this;
      this.fjList = [];
      this.dataForm.fjId = "";
      this.$http
        .get(`/house/houseinfo/fjList`, {
          params: {
            dyId: id
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.fjList = res.data;
        })
        .catch(() => {});
    },

    //房间列表change
    FJlistChange(val) {
      this.getFjInfo();
    },

    //获取房间信息
    getFjInfo() {
      let _this = this;
      this.$http
        .get(`/house/houseinfo/fjInfo`, {
          params: {
            id: this.dataForm.fjId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
          this.dataForm.id = null;
        })
        .catch(() => {});
    },
    Setprices() {
      this.dataForm.prices = this.dataForm.area * this.dataForm.price;
    },

    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.dataForm.id = null;
          this.$http
            .post("/house/houseinfo/", this.dataForm)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                  this.$refs["dataForm"].resetFields();
                }
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    )
  },
  watch: {
    "dataForm.transactionType": {
      handler(nowVal, oldVal) {
        if (nowVal.toString() == "1") {
          this.dataForm.price = this.dataForm.leasePrice;
          this.dataForm.prices = this.dataForm.leasePrices;
        } else if (nowVal.toString() == "0") {
          this.dataForm.price = this.dataForm.sellPrice;
          this.dataForm.prices = this.dataForm.sellPrices;
        }
      }
    }
  }
  //
};
</script>
